Merge pull request #1099 from purposeindustries/fetch-tumblr-posts

Fetch post's data after publishing to tumblr

Andrew Cantino 9 years ago
parent
commit
b59ac85132
2 changed files with 99 additions and 37 deletions
  1. 20 7
      app/models/agents/tumblr_publish_agent.rb
  2. 79 30
      spec/models/agents/tumblr_publish_agent_spec.rb

+ 20 - 7
app/models/agents/tumblr_publish_agent.rb

@@ -17,9 +17,9 @@ module Agents
17 17
 
18 18
       **Required fields:**
19 19
 
20
-      `blog_name` Your Tumblr URL (e.g. "mustardhamsters.tumblr.com") 
20
+      `blog_name` Your Tumblr URL (e.g. "mustardhamsters.tumblr.com")
21 21
 
22
-      `post_type` One of [text, photo, quote, link, chat, audio, video] 
22
+      `post_type` One of [text, photo, quote, link, chat, audio, video]
23 23
 
24 24
 
25 25
       -------------
@@ -35,13 +35,13 @@ module Agents
35 35
       * `format` html, markdown
36 36
       * `slug` short text summary at end of the post URL
37 37
 
38
-      **Text** `title` `body` 
38
+      **Text** `title` `body`
39 39
 
40 40
       **Photo** `caption` `link`  `source`
41 41
 
42 42
       **Quote** `quote` `source`
43 43
 
44
-      **Link** `title` `url` `description` 
44
+      **Link** `title` `url` `description`
45 45
 
46 46
       **Chat** `title` `conversation`
47 47
 
@@ -105,19 +105,25 @@ module Agents
105 105
         options = interpolated(event)['options']
106 106
         begin
107 107
           post = publish_post(blog_name, post_type, options)
108
+          if !post.has_key?('id')
109
+            log("Failed to create #{post_type} post on #{blog_name}: #{post.to_json}, options: #{options.to_json}")
110
+            return
111
+          end
112
+          expanded_post = get_post(blog_name, post["id"])
108 113
           create_event :payload => {
109 114
             'success' => true,
110 115
             'published_post' => "["+blog_name+"] "+post_type,
111 116
             'post_id' => post["id"],
112 117
             'agent_id' => event.agent_id,
113
-            'event_id' => event.id
118
+            'event_id' => event.id,
119
+            'post' => expanded_post
114 120
           }
115 121
         end
116 122
       end
117 123
     end
118 124
 
119
-    def publish_post(blog_name, post_type, options)      
120
-      options_obj = { 
125
+    def publish_post(blog_name, post_type, options)
126
+      options_obj = {
121 127
           :state => options['state'],
122 128
           :tags => options['tags'],
123 129
           :tweet => options['tweet'],
@@ -159,5 +165,12 @@ module Agents
159 165
         tumblr.video(blog_name, options_obj)
160 166
       end
161 167
     end
168
+
169
+    def get_post(blog_name, id)
170
+      obj = tumblr.posts(blog_name, {
171
+        :id => id
172
+      })
173
+      obj["posts"].first
174
+    end
162 175
   end
163 176
 end

+ 79 - 30
spec/models/agents/tumblr_publish_agent_spec.rb

@@ -1,38 +1,87 @@
1 1
 require 'rails_helper'
2 2
 
3 3
 describe Agents::TumblrPublishAgent do
4
-  before do
5
-    @opts = {
6
-      :blog_name => "huginnbot.tumblr.com",
7
-      :post_type => "text",
8
-      :expected_update_period_in_days => "2",
9
-      :options => {
10
-        :title => "{{title}}",
11
-        :body => "{{body}}",
12
-      },
13
-    }
14
-
15
-    @checker = Agents::TumblrPublishAgent.new(:name => "HuginnBot", :options => @opts)
16
-    @checker.service = services(:generic)
17
-    @checker.user = users(:bob)
18
-    @checker.save!
19
-
20
-    @event = Event.new
21
-    @event.agent = agents(:bob_weather_agent)
22
-    @event.payload = { :title => "Gonna rain...", :body => 'San Francisco is gonna get wet' }
23
-    @event.save!
24
-
25
-    stub.any_instance_of(Agents::TumblrPublishAgent).tumblr {
26
-      stub!.text(anything, anything) { { "id" => "5" } }
27
-    }
4
+  describe "Should create post" do
5
+    before do
6
+      @opts = {
7
+        :blog_name => "huginnbot.tumblr.com",
8
+        :post_type => "text",
9
+        :expected_update_period_in_days => "2",
10
+        :options => {
11
+          :title => "{{title}}",
12
+          :body => "{{body}}",
13
+        },
14
+      }
15
+
16
+      @checker = Agents::TumblrPublishAgent.new(:name => "HuginnBot", :options => @opts)
17
+      @checker.service = services(:generic)
18
+      @checker.user = users(:bob)
19
+      @checker.save!
20
+
21
+      @event = Event.new
22
+      @event.agent = agents(:bob_weather_agent)
23
+      @event.payload = { :title => "Gonna rain...", :body => 'San Francisco is gonna get wet' }
24
+      @event.save!
25
+
26
+      @post_body = {
27
+        "id" => 5,
28
+        "title" => "Gonna rain...",
29
+        "link" => "http://huginnbot.tumblr.com/gonna-rain..."
30
+      }
31
+      stub.any_instance_of(Agents::TumblrPublishAgent).tumblr {
32
+        obj = Object.new
33
+        stub(obj).text(anything, anything) { { "id" => "5" } }
34
+        stub(obj).posts("huginnbot.tumblr.com", {:id => "5"}) {
35
+          {"posts" => [@post_body]}
36
+        }
37
+      }
38
+
39
+    end
40
+
41
+    describe '#receive' do
42
+      it 'should publish any payload it receives' do
43
+        Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
44
+        expect(@checker.events.count).to eq(1)
45
+        expect(@checker.events.first.payload['post_id']).to eq('5')
46
+        expect(@checker.events.first.payload['published_post']).to eq('[huginnbot.tumblr.com] text')
47
+        expect(@checker.events.first.payload["post"]).to eq @post_body
48
+      end
49
+    end
28 50
   end
29 51
 
30
-  describe '#receive' do
31
-    it 'should publish any payload it receives' do
32
-      Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
33
-      expect(@checker.events.count).to eq(1)
34
-      expect(@checker.events.first.payload['post_id']).to eq('5')
35
-      expect(@checker.events.first.payload['published_post']).to eq('[huginnbot.tumblr.com] text')
52
+  describe "Should handle tumblr error" do
53
+    before do
54
+      @opts = {
55
+        :blog_name => "huginnbot.tumblr.com",
56
+        :post_type => "text",
57
+        :expected_update_period_in_days => "2",
58
+        :options => {
59
+          :title => "{{title}}",
60
+          :body => "{{body}}",
61
+        },
62
+      }
63
+
64
+      @checker = Agents::TumblrPublishAgent.new(:name => "HuginnBot", :options => @opts)
65
+      @checker.service = services(:generic)
66
+      @checker.user = users(:bob)
67
+      @checker.save!
68
+
69
+      @event = Event.new
70
+      @event.agent = agents(:bob_weather_agent)
71
+      @event.payload = { :title => "Gonna rain...", :body => 'San Francisco is gonna get wet' }
72
+      @event.save!
73
+
74
+      stub.any_instance_of(Agents::TumblrPublishAgent).tumblr {
75
+        stub!.text(anything, anything) { {"status" => 401,"msg" => "Not Authorized"} }
76
+      }
77
+    end
78
+
79
+    describe '#receive' do
80
+      it 'should publish any payload it receives and handle error' do
81
+        Agents::TumblrPublishAgent.async_receive(@checker.id, [@event.id])
82
+        puts "events #{@checker.events.inspect}"
83
+        expect(@checker.events.count).to eq(0)
84
+      end
36 85
     end
37 86
   end
38 87
 end